<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//
namespace AuctionBack\Controller;

use AuctionCore\Common\AjaxPage;
use AuctionCore\Common\PhpRedis;

class LotNumberFileController extends BaseController
{
    private $lotNumberFileLogic;

    private $empId;

    public function __construct()
    {
        parent::__construct();
        $this->lotNumberFileLogic = D('LotNumberFile', 'Logic');
        $this->empId = $this->user_info['emp_id'];
        $lotNumberCityList = C('CLCW_LOT_NUMBER_CITY');
        $this->assign('lot_number_city_list', $lotNumberCityList);
    }

    public function index()
    {
        $this->display();
    }

    public function lotNumberFileList()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = [];
        $params['title'] = I('get.title', '', 'trim');
        $params['city'] = I('get.city', '');
        $params['index_type'] = I('get.index_type/d', 0);
        $params['status'] = I('get.status/d', 0);
        $params['start_time'] = I('get.start_time');
        $params['end_time'] = I('get.end_time');
        $result = $this->lotNumberFileLogic->getLotNumberFileList($page, $size, $params);
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();   // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->assign('total', $result['total']);
        $this->assign('reward_total', $result['reward_total']);
        $this->display();

    }


    /**
     * 添加文件
     */
    public function addLotNumberFile()
    {
        $title = I('post.title', '', 'trim');
        if (empty($title)) {
            $this->helpJsonResult(400, "抱歉，标题不能为空");
        }
        $city = I('post.city', '');
        if (empty($city)) {
            $this->helpJsonResult(400, "抱歉，请选择城市");
        }
        $indexType = I('post.index_type', '');
        if (empty($indexType)) {
            $this->helpJsonResult(400, "抱歉，请选择指标类型");
        }
        $fileUrl = I('post.file_url', '', 'trim');
        if (empty($fileUrl)) {
            $this->helpJsonResult(400, '抱歉，请填写文件URL');
        }
        $parseFileUrl = pathinfo($fileUrl);
        if ($parseFileUrl['extension'] != 'pdf') {
            $this->helpJsonResult(400, '抱歉，请填写后缀为.PDF的URL');
        }
        $remark = I('post.remark', '', 'trim');
        $data['emp_id'] = $this->empId;
        $data['title'] = $title;
        $data['city_code'] = $city;
        $data['index_type'] = $indexType;
        $data['url'] = $fileUrl;
        $data['remark'] = $remark;
        $res = $this->lotNumberFileLogic->addLotNumberFile($data);
        if ($res) {
            $redis = new PhpRedis(0);
            $keyName = getRedisKeyByCode('REDIS_KEY', '100000');
            $redis->lpush($keyName,
                json_encode(
                    [
                        'bizContent' => [
                            "lotId" => (int)$res
                        ],
                        'bizType'    => 12
                    ]
                )
            );
            $this->helpJsonResult(200, '添加成功');
        } else {
            $this->helpJsonResult(400, '添加失败');
        }
    }

    /**
     * 编辑页面
     */
    public function edit()
    {
        $id = I('get.id/d', 0);
        if ($id < 1) {
            $this->redirect('/LotNumberFile/index', '', 3, '抱歉，信息不存在');
        }
        $detail = $this->lotNumberFileLogic->getLotNumberFileById($id);
        if ($detail['status'] < 1) {
            $this->redirect('/LotNumberFile/index', '', 3, '抱歉，信息不存在');
        }
        $this->assign('detail', $detail);
        $this->display();
    }

    //编辑提交
    public function editLotNumberFile()
    {
        $id = I('post.lot_id/d', 0);
        if ($id < 1) {
            $this->helpJsonResult('400', '请求参数不正确');
        }
        $lotNumberInfo = $this->lotNumberFileLogic->getLotNumberFileById($id);
        $title = I('post.title', '', 'trim');
        if (empty($title)) {
            $this->helpJsonResult(400, "抱歉，标题不能为空");
        }
        $city = I('post.city', '');
        if (empty($city)) {
            $this->helpJsonResult(400, "抱歉，请选择城市");
        }
        $indexType = I('post.index_type', '');
        if (empty($indexType)) {
            $this->helpJsonResult(400, "抱歉，请选择指标类型");
        }
        $fileUrl = I('post.file_url', '', 'trim');
        if (empty($fileUrl)) {
            $this->helpJsonResult(400, '抱歉，请填写文件URL');
        }
        $parseFileUrl = pathinfo($fileUrl);
        if ($parseFileUrl['extension'] != 'pdf') {
            $this->helpJsonResult(400, '抱歉，请填写后缀为.PDF的URL');
        }
        $remark = I('post.remark', '', 'trim');
        $data['id'] = $id;
        $data['emp_id'] = $this->empId;
        $data['title'] = $title;
        $data['city_code'] = $city;
        $data['index_type'] = $indexType;
        $data['url'] = $fileUrl;
        $data['remark'] = $remark;
        if ($lotNumberInfo['url'] != $fileUrl) {
            $data['status'] = 1;
        }

        $res = $this->lotNumberFileLogic->editLotNumberFile($data);
        if ($res) {
            if ($lotNumberInfo['url'] != $fileUrl) {
                $redis = new PhpRedis(0);
                $keyName = getRedisKeyByCode('REDIS_KEY', '100000');
                $redis->lpush($keyName,
                    json_encode(
                        [
                            'bizContent' => [
                                "lotId" => (int)$res
                            ],
                            'bizType'    => 12
                        ]
                    )
                );
            }
            $this->helpJsonResult(200, '编辑成功');
        } else {
            $this->helpJsonResult(400, '编辑失败');
        }
    }

    public function delLotNumberFile()
    {
        $lotId = I('post.lot_id/d', 0);
        if ($lotId < 1) {
            $this->helpJsonResult(400, '抱歉，记录不存在');
        }
        $res = $this->lotNumberFileLogic->delLotNumberFile($lotId);
        if ($res) {
            $this->helpJsonResult(200, '操作成功');
        } else {
            $this->helpJsonResult(400, '操作成功');
        }
    }
}